# -*- coding: utf-8 -*-
"""
用户系统 - Django Admin配置
"""

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from .models import User, UserFollow, PrivateMessage


@admin.register(User)
class UserAdmin(BaseUserAdmin):
    """用户管理"""
    list_display = ('username', 'nickname', 'email', 'phone', 'points', 'is_staff', 'created_at')
    list_filter = ('is_staff', 'is_superuser', 'is_active', 'created_at')
    search_fields = ('username', 'nickname', 'email', 'phone')
    ordering = ('-created_at',)
    
    fieldsets = BaseUserAdmin.fieldsets + (
        ('额外信息', {'fields': ('nickname', 'phone', 'avatar', 'signature', 'interests', 'points')}),
        ('隐私设置', {'fields': ('privacy_settings',)}),
        ('第三方登录', {'fields': ('wechat_openid', 'google_id', 'facebook_id')}),
        ('统计信息', {'fields': ('created_activities_count', 'joined_activities_count')}),
    )


@admin.register(UserFollow)
class UserFollowAdmin(admin.ModelAdmin):
    """用户关注管理"""
    list_display = ('follower', 'following', 'created_at')
    list_filter = ('created_at',)
    search_fields = ('follower__username', 'following__username')
    raw_id_fields = ('follower', 'following')


@admin.register(PrivateMessage)
class PrivateMessageAdmin(admin.ModelAdmin):
    """私信管理"""
    list_display = ('sender', 'receiver', 'content_preview', 'is_read', 'created_at')
    list_filter = ('is_read', 'created_at')
    search_fields = ('sender__username', 'receiver__username', 'content')
    raw_id_fields = ('sender', 'receiver')
    
    def content_preview(self, obj):
        return obj.content[:50] + '...' if len(obj.content) > 50 else obj.content
    content_preview.short_description = '消息内容'
